function [peaks] = findpeaks(bestRes) 

[zero, one, two, three, four, five, six, seven, eight, nine] = readImages('../datasets/zip.train');

Peaks = zeros(size(bestRes(:,:,1)));

figure;
for r = 1:10
    image = squeeze(bestRes(:,:,r));
    image = (image-min(min(image)))./(max(max(image))-min(min(image)));
    subplot(2,5,r); imagesc(image); colormap gray;
end

figure;
for r = 1:10   
    image = squeeze(bestRes(:,:,r));
    image = (image-min(min(image)))./(max(max(image))-min(min(image)));
    peakImage = zeros(size(image));
    for i = 1:size(image,1)
        for j = 1:size(image,2)
            boolPix = 1;
            for p = i-1:i+1
                for q = j-1:j+1
                    if (p>0 && p<=size(image,1) && q>0 && q<=size(image,2)) 
                        if ((i~=p)||(j~=q)) && (image(i,j)<image(p,q))
                            boolPix = 0;
                        end
                    end
                end
            end

            if(boolPix~=0)
                peakImage(i,j) = image(i,j);
            end
        end
    end    
    
    % store Original image details
    if(r == 1)
        Oimages = zero;
    elseif(r == 2)
        Oimages = one;
    elseif(r == 3)
        Oimages = two;
    elseif(r == 4)
        Oimages = three;
    elseif(r == 5)
        Oimages = four;
    elseif(r == 6)
        Oimages = five;
    elseif(r == 7)
        Oimages = six;
    elseif(r == 8)
        Oimages = seven;
    elseif(r == 9)
        Oimages = eight;
    elseif(r == 10)
        Oimages = nine;        
    end 
    
    peakImage = peakImage./size(Oimages,3);
    
    Peaks = Peaks + peakImage;    
    subplot(2,5,r); imagesc(peakImage); colormap gray;    
end

PeaksOnPeaks = zeros(size(Peaks));

figure; imagesc(Peaks); colormap gray;

for i = 1:size(Peaks,1)
    for j = 1:size(Peaks,2)
        boolPix = 1;
        for p = i-1:i+1
            for q = j-1:j+1
                if (p>0 && p<=size(Peaks,1) && q>0 && q<=size(Peaks,2))
                    if ((i~=p)||(j~=q)) && (Peaks(i,j)<Peaks(p,q))
                        boolPix = 0;
                    end
                end
            end
        end

        if(boolPix~=0)
            PeaksOnPeaks(i,j) = Peaks(i,j);
        end
    end
end

figure; imagesc(PeaksOnPeaks); colormap gray;
[y,x,v]= find(PeaksOnPeaks>0);
hold on; scatter(x,y);

peaks = [x'; y'];

end
